package org.lql.algo.codecrush.week005;

import org.lql.algo.common.ListNode;

import java.util.Arrays;

/**
 * @author: liangqinglong
 * @date: 2025-08-05 17:52
 * @description: LCR 123. 图书整理 I <a href="https://leetcode.cn/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/description/">...</a>
 **/
public class ReverseBookList {

	/**
	 * 书店店员有一张链表形式的书单，每个节点代表一本书，节点中的值表示书的编号。
	 * 为更方便整理书架，店员需要将书单倒过来排列，就可以从最后一本书开始整理，逐一将书放回到书架上。请倒序返回这个书单链表。
	 * <p>
	 * <p>
	 * <p>
	 * 示例 1：
	 * <p>
	 * 输入：head = [3,6,4,1]
	 * <p>
	 * 输出：[1,4,6,3]
	 * <p>
	 * <p>
	 * 提示：
	 * <p>
	 * 0 <= 链表长度 <= 10000
	 */
	public int[] reverseBookList(ListNode head) {
		ListNode cur = head;
		int size = 0;
		while (cur != null) {
			size++;
			cur = cur.next;
		}
		int[] res = new int[size];
		cur = head;
		for (int i = size - 1; i >= 0; i--) {
			res[i] = cur.val;
			cur = cur.next;
		}
		return res;
	}

	public static void main(String[] args) {
		ReverseBookList reverseBookList = new ReverseBookList();
		ListNode head = new ListNode(3);
		head.next = new ListNode(6);
		head.next.next = new ListNode(4);
		head.next.next.next = new ListNode(1);
		int[] res = reverseBookList.reverseBookList(head);
		System.out.println(Arrays.toString(res));
	}
}
